package com.zixuan.gulicollege.uservice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zixuan.gulicollege.common_utils.ConstantConfigInfo;
import com.zixuan.gulicollege.common_utils.JwtUtils;
import com.zixuan.gulicollege.common_utils.Md5Utils;
import com.zixuan.gulicollege.common_utils.ResultCode;
import com.zixuan.gulicollege.entity.ucenter.UcenterMember;
import com.zixuan.gulicollege.entity.ucenter.vo.RegisterVO;
import com.zixuan.gulicollege.exception_handler.ConstantExceptionInfo;
import com.zixuan.gulicollege.exception_handler.GuliException;
import com.zixuan.gulicollege.uservice.mapper.UcenterMemberMapper;
import com.zixuan.gulicollege.uservice.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author zixuan
 * @since 2021-05-19
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();

    @Override
    public String loginUser(UcenterMember user) {
        String mobile = user.getMobile();
        String password = user.getPassword();

        QueryWrapper<UcenterMember> userWrapper = new QueryWrapper<>();
        userWrapper.eq("mobile", mobile);
        UcenterMember mobileMember = this.baseMapper.selectOne(userWrapper);

        // 判断手机号是否存在
        if (mobileMember == null) {
            throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.PHONE_AND_PASSWORD_ERROR);
        }

        // 判断密码是否相同
        if (!Md5Utils.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.PHONE_AND_PASSWORD_ERROR);
        }

        // 判断用户是否被禁用
        if (mobileMember.getIsDisabled()) {
            throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.ACCOUNT_BLOCKED);
        }

        // 生成token值
        return JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
    }

    @Override
    public void registerUser(RegisterVO registerVO) {
        // 获取注册信息
        String code = registerVO.getCode();
        String mobile = registerVO.getMobile();
        String nickname = registerVO.getNickname();

        // 判断手机号是已经注册
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer mobileCount = baseMapper.selectCount(wrapper);
        if (mobileCount > 0) {
            throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.PHONE_ALREADY_EXISTS);
        }

        // 判断用户名称是否存在
        wrapper = new QueryWrapper<UcenterMember>();
        wrapper.eq("nickname", nickname);
        Integer nickName = baseMapper.selectCount(wrapper);
        if (nickName > 0) {
            throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.USER_NAME_ALREADY_EXISTS);
        }

        // 比较验证码是否相同
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!Objects.equals(code, redisCode)) {
            throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.VERIFICATION_CODE_ERROR);
        }

        // 查看验证码是否过期
        Long codeTime = redisTemplate.opsForValue().getOperations().getExpire(mobile);
        if (Objects.equals(codeTime, ConstantConfigInfo.NO_REDIS_KEY)){
            throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.VERIFICATION_CODE_EXPIRE);
        }
        // 删除验证码
        redisTemplate.delete(mobile);

        UcenterMember ucenterMember = new UcenterMember();
        BeanUtils.copyProperties(registerVO, ucenterMember);
        // MD5密码加密
        ucenterMember.setPassword(Md5Utils.encrypt(ucenterMember.getPassword()));
        // 设置默认头像
        ucenterMember.setAvatar("https://cdn.jsdelivr.net/gh/CodeZixuan/Blog_Images/Generic/bg.png");
        baseMapper.insert(ucenterMember);
    }
}
